package com.acme.domain.account;

import com.acme.exceptions.NoEnoughFundsException;

public class SavingAccount extends AbstractAccount {

    public SavingAccount(final int id, final double amount) {
        super(id, amount);
    }

    @Override
    public void deposit(final double amount) throws IllegalArgumentException {
        if (amount < 0) {
            throw new IllegalArgumentException("Amount can not be negative");
        }
        this.balance += amount;
    }

    @Override
    public void withdraw(final double amount) throws NoEnoughFundsException {
        if (amount < 0) {
            throw new IllegalArgumentException("Amount can not be negative");
        }
        if (this.balance - amount >= 0) {
            this.balance -= amount;
        } else {
            throw new NoEnoughFundsException(amount);
        }

    }

    public AccountType getAccountType() {
        return AccountType.SAVING;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        SavingAccount that = (SavingAccount) o;

        if (Double.compare(that.balance, balance) != 0) return false;
        if (id != that.id) return false;
        if (state != that.state) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = id;
        long temp = balance != +0.0d ? Double.doubleToLongBits(balance) : 0L;
        result = 31 * result + (int) (temp ^ (temp >>> 32));
        result = 31 * result + (state != null ? state.hashCode() : 0);
        return result;
    }

    @Override
    public String toString() {
        return "SavingAccount{" +
                "id=" + id +
                ", balance=" + balance +
                ", state=" + state +
                '}';
    }
}
